//
//  CellOfTVCDetailVideo.m
//  To Feel
//
//  Created by 曹宇 on 16/7/14.
//  Copyright © 2016年 caoyu. All rights reserved.
//

#import "CellOfTVCDetailVideo.h"
#import "Masonry.h"
#import "ModelOfSecondTVC.h"
#import "UIImageView+WebCache.h"
@implementation CellOfTVCDetailVideo

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatSubview];
    }
    return self;
}

- (void)setModel:(ModelOfSecondTVC *)model {
    
    
    if ([model.type isEqualToString:@"banner2"]) {
        
        NSURL *url = [NSURL URLWithString:[model.data objectForKey:@"image"]];
        [self.imgOfPlay sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"holder4"]];
        self.viewOfImg.backgroundColor = [UIColor clearColor];
        
    }else {
        
        [self setData:model];
        
    }
}

- (void)setData:(ModelOfSecondTVC *)model {
    
    NSURL *url = [NSURL URLWithString:[[model.data objectForKey:@"cover"]objectForKey:@"feed"]];
    [self.imgOfPlay sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"holder4"]];
    self.labelOfTitle.text = [model.data objectForKey:@"title"];
    NSInteger time = [[model.data objectForKey:@"duration"] integerValue];
    
    NSInteger minute = time / 60;
    NSInteger second = time % 60;
    
    self.labelOfInfo.text = [NSString stringWithFormat:@"#%@ / %02ld'%02ld''", [model.data objectForKey:@"category"], minute, second];
    
}

#pragma mark - creatSubview

- (void)creatSubview {
    
    self.imgOfPlay = [[UIImageView alloc] init];
    
    [self.contentView addSubview:self.imgOfPlay];
    
    [self.imgOfPlay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.contentView);
    }];
    
    self.viewOfImg = [[UIView alloc] init];
    
    [self.contentView addSubview:self.viewOfImg];
    
    [self.viewOfImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.imgOfPlay);
    }];
    
    self.labelOfTitle = [[UILabel alloc] init];
    
    [self.contentView addSubview:self.labelOfTitle];
    
    [self.labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).with.offset(-10);
        make.height.offset(25);
        make.centerX.equalTo(self);
        
    }];
    
    self.labelOfInfo = [[UILabel alloc] init];
    
    [self.contentView addSubview:self.labelOfInfo];
    
    [self.labelOfInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(20);
        make.top.equalTo(self.labelOfTitle.mas_bottom).with.offset(10);
        make.centerX.equalTo(self);
    }];
    
    [self configSubview];
}

/** configSubview */
- (void)configSubview {
    
    self.viewOfImg.backgroundColor = [UIColor clearColor];
    self.labelOfTitle.font = [UIFont systemFontOfSize:16 weight:1];
    self.labelOfInfo.font = [UIFont systemFontOfSize:12];
    self.labelOfTitle.textColor = [UIColor whiteColor];
    self.labelOfInfo.textColor = [UIColor whiteColor];
    
}

- (CGFloat)cellOffset {
    
    CGRect centerToWindow = [self convertRect:self.bounds toView:self.window];
    CGFloat centerY = CGRectGetMidY(centerToWindow);
    CGPoint windowCenter = self.superview.center;
    
    CGFloat cellOffsetY = centerY - windowCenter.y;
    
    CGFloat offsetDig =  cellOffsetY / self.superview.frame.size.height *2;
    CGFloat offset =  -offsetDig * (SCREEN_HEIGHT/1.7 - 250)/2;
    
    CGAffineTransform transY = CGAffineTransformMakeTranslation(0,offset);
    
    //    self.titleLabel.transform = transY;
    //    self.littleLabel.transform = transY;
    
    self.viewOfImg.transform = transY;
    return offset;
}

@end
